<template>
  <h1>员工列表综合练习</h1>
<!--  提交数据-->
  <el-form style="width: 600px;margin: 0 auto;" >
    <el-form-item label="姓名">
      <el-input placeholder="请输入员工姓名" v-model="user.name"></el-input>
    </el-form-item>
    <el-form-item label="工资">
      <el-input placeholder="请输入员工工资" v-model="user.salary"></el-input>
    </el-form-item>
    <el-form-item label="岗位">
      <el-input placeholder="请输入员工岗位" v-model="user.job"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="userArr" style="width: 600px;margin: 0 auto;">
    <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
    <el-table-column prop="name" label="姓名"  align="center"></el-table-column>
    <el-table-column prop="salary" label="工资"  align="center"></el-table-column>
    <el-table-column prop="job" label="岗位"  align="center"></el-table-column>
    <el-table-column label="操作"  align="center">
      <template #default="scope">
        <el-button type="danger" @click="del(scope.$index,scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup>
import {ref} from "vue";
// 提交数据
const user=ref(
    {name:"",salary:'',job:''}
);
//模拟后端数据
const userArr=ref([

]);
// 提交数据
const submit=()=>{
  //数据验证
  //null undefined NaN '' 0 只有这5个特殊值转为布尔是false  '空格'转布尔是true!
  if (!user.value.name||!user.value.name.trim()
      ||!user.value.salary||!user.value.salary.trim()
      ||!user.value.job||!user.value.job.trim()
  ){
    alert("请填写完整");
    return;
  }
  userArr.value.push(user.value);
  // 清空数据
  user.value={name:"",salary:'',job:''};
  console.log(userArr.value);
};
const del=(index,row)=>{
  //提示信息
  if (confirm('确认'+row.name+'吗？')){
    //删除
    userArr.value.splice(index,1);
  }
};
</script>

<style scoped>

</style>